<script lang="ts" setup>
import { onLaunch, onShow, onHide } from '@dcloudio/uni-app'
import { useAccountStore } from '@/store/modules/account'
import { getStore } from '@/utils/storage'
import { STORAGE_KEYS } from '@/common/constants'
/* 检查更新 */
const onUpdate = () => {
  const updateManager = wx.getUpdateManager()
  updateManager.onCheckForUpdate(function ({ hasUpdate }: { hasUpdate: boolean }) {
    if (hasUpdate) {
      updateManager.onUpdateReady(({ errMsg }: { errMsg: string }) => {
        if (errMsg) return
        wx.showModal({
          title: '更新提示',
          content: '新版本已经准备好，是否重启应用？',
          showCancel: false,
          success() {
            updateManager.applyUpdate()
          }
        })
      })
    }
  })
}

onLaunch(async () => {
  uni.hideTabBar()
  const accountStore = useAccountStore()
  // 判断是否登录
  const token = getStore(STORAGE_KEYS.TOKEN)
  if (token) {
    accountStore.getUserInfoByToken()
  } else {
    const needGetPhone = await accountStore.checkNeedGetPhone()
    console.log('[ needGetPhone ] >', needGetPhone)
    if (!needGetPhone) {
      // 无需获取手机号，直接登录
      accountStore.loginAction({})
    }
  }
  // onUpdate()
})
onShow(() => {
  console.log('App Show')
})
onHide(() => {
  console.log('App Hide')
})
</script>
<style lang="scss">
@import '@/styles/index.scss';
@import 'tailwindcss/base';
@import 'tailwindcss/utilities';
</style>
